# -*- coding:utf-8 -*-
import json

# @Time    : 2023/5/16 02:21
# @Author  : zengwenjia
# @Email   : zengwenjia@lingxi.ai
# @File    : generate_bot_dialogue.py
# @Software: LLM_internal

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
import pandas as pd
import asyncio
from bot.insurance_sales_huize.sales import Sales
# from bot.insurance_sales_huize.user import User
import uuid
from data_generate import utils
import traceback
import random
from loguru import logger


# 用户query模拟生成
async def mock_bot_dialogue_by_query(path, taget_num=1):
    conversation_history = []
    sales = Sales()
    result = asyncio.run(sales.async_reply(conversation_history, "123", content=user_input))
    conversation_dict = {}
    conversation_dict["role"] = "assistant"
    conversation_dict["content"] = result
    conversation_history.append(conversation_dict)
    logger.info('历史对话缓存:{}'.format(conversation_history))
    if '再见' in result or '马上为您服务' in result:
        logger.info("*******对话已经结束：转人/挂机*******\n\n\n")
        conversation_history = []
        user_input = ''
    else:
        user_input = input("请输入用户的对话：")
        conversation_dict = {}
        conversation_dict["role"] = "user"
        conversation_dict["content"] = user_input
        conversation_history.append(conversation_dict)


def conversation2csv(file_path):
    datas = utils.jload(file_path)
    df = pd.DataFrame(columns=['角色', '内容'])

    for data in datas:
        messages = data['messages']
        for text in messages:
            df = df._append(
                {"角色": text['role'],
                 "内容": text['content']},
                ignore_index=True)
        df = df.reset_index(drop=True)._append(
            {"角色": "",
             "内容": ""},
            ignore_index=True)
    df.to_csv(file_path.replace('.json', '.csv'))


if __name__ == '__main__':
    import datetime

    for i in range(20):
        now_date = datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")
        file_path = "../data_set/bot_dialogue/bot_dialogue_" + now_date + ".json"  # 文件夹路径
        asyncio.run(mock_bot_dialogue_by_query(file_path))
        conversation2csv(file_path)

    # get_faq_corpus("/Users/cy/Downloads/222.csv")
